﻿
; (function ($)
{
	$.fn.quickNav = function (options)
	{
		var $this = this;
		options = $.extend({}, $.fn.quickNav.defaults, options);
		var currentElement = null;
		var boundCloseButton = false;

		return $this.each(function(i)
		{
			var $element = $(this);
			var ajaxUrl = null;
			var loaded = false;
			var elementOptions = $.extend(true, {}, options);
			var labelText = $element.html();
			$element.html('');
			var icon = $('<div>').addClass(elementOptions.iconClassName).appendTo($element);
			var label = $('<div>').addClass(elementOptions.labelClassName).html(labelText).appendTo($element);
			var closedWidth = $element.width();
			elementOptions.iconHoverClassName = elementOptions.iconClassName + "Hover";
			
			if ($.isFunction(options.callbacks.onLoad))
			{
				options.callbacks.onLoad.call($this, $element, elementOptions);
			}

			elementOptions.functions =
			{
				elementMouseover: function()
				{
					Debugger.write("mouse over");

					if ($.isFunction(options.callbacks.onMouseover))
					{
						options.callbacks.onMouseover.call($this, $element, elementOptions);
					}

					if (!elementOptions.containerOpen)
					{
						var openWidth = label.width() + closedWidth + parseInt(label.css('padding-right'));
						
						elementOptions.functions.setIconState(true);

						$element.stop().animate({ width: openWidth }, elementOptions.animationSpeed);
					}
				},
				elementMouseout: function()
				{
					Debugger.write("mouse out");

					if ($.isFunction(options.callbacks.onMouseout))
					{
						options.callbacks.onMouseout.call($this, $element, elementOptions);
					}

					if (!elementOptions.containerOpen)
					{
						$element.stop().animate({ width: closedWidth }, elementOptions.animationSpeed, function()
						{
							$element.removeClass(elementOptions.itemOpenedClassName);
							elementOptions.functions.setIconState(false);
						});
					}
				},
				click: function()
				{
					Debugger.write("click");

					if ($.isFunction(options.callbacks.onClick))
					{
						options.callbacks.onClick.call($this, $element, elementOptions);
					}

					this.openContainer();
				},
				openContainer: function()
				{
					if (options.containerOpen && elementOptions.persistOnClick)
					{
						if (currentElement && currentElement !== elementOptions && currentElement.containerOpen)
						{
							currentElement.functions.hideContentContainer(function()
							{
								elementOptions.functions.openContainer();
								this.elementMouseout();
							});
						}
					}
					else
					{
						loaded = $($element).data("loaded");
						ajaxUrl = $($element).attr("ajaxUrl");

						if (elementOptions.persistOnClick && ajaxUrl)
						{
							$element.addClass(elementOptions.itemOpenedClassName);
							$(elementOptions.contentContainerLabel).text(labelText);

							if (!loaded)
							{
								elementOptions.tag = $("<div>").addClass("quickNavContent").appendTo(elementOptions.contentContainer);
								$(elementOptions.tag).ajaxLoading({
									ajaxUrl: ajaxUrl
								});
							}
							else
							{
								elementOptions.tag.appendTo(elementOptions.contentContainer);
							}

							if (!boundCloseButton)
							{
								$(elementOptions.contentContainer + " #quickNavContentCloseButton").unbind('click');
								$(elementOptions.contentContainer + " #quickNavContentCloseButton").click(function()
								{
									if (currentElement && currentElement.containerOpen)
									{
										currentElement.functions.closeContainer();
									}
								});

								boundCloseButton = true;
							}

							if (!elementOptions.containerOpen)
							{
								Debugger.write("container opened");

								if ($.isFunction(options.callbacks.onOpen))
								{
									options.callbacks.onOpen.call($this, $element, elementOptions);
								}

								this.displayContentContainer();
							}

							$($element).data("loaded", true);
						}
					}
				},
				closeContainer: function()
				{
					this.hideContentContainer();
				},
				displayContentContainer: function()
				{
					setTimeout(function()
					{
						$($element).width(closedWidth);
					},
					elementOptions.animationSpeed / 4);
					
					currentElement = elementOptions;
					elementOptions.containerOpen = true;

					$(elementOptions.contentContainer).stop().animate({ width: "toggle" }, elementOptions.animationSpeed, function()
					{
						options.containerOpen = true;
					});
				},
				hideContentContainer: function(callback)
				{
					Debugger.write("container closed");

					if ($.isFunction(options.callbacks.onClose))
					{
						options.callbacks.onClose.call($this, $element, elementOptions);
					}

					$(elementOptions.contentContainer).stop().animate({ width: "toggle" }, elementOptions.animationSpeed, function()
					{
						elementOptions.tag.remove();
						$(elementOptions.contentContainer).hide();
						$element.removeClass(elementOptions.itemOpenedClassName);
						elementOptions.functions.setIconState(false);
						elementOptions.containerOpen = false;
						options.containerOpen = false;
						currentElement = null;

						if ($.isFunction(callback))
						{
							callback();
						}
					});
				},
				setIconState: function(isOver)
				{
					icon.removeClass(isOver ? elementOptions.iconClassName : elementOptions.iconHoverClassName);
					icon.addClass(isOver ? elementOptions.iconHoverClassName : elementOptions.iconClassName);
				}
			};

			$element.unbind("hover");
			$element.hover(function()
			{
				elementOptions.functions.elementMouseover();
			},
			function()
			{
				elementOptions.functions.elementMouseout();
			});

			$(elementOptions.contentContainer).css(
				"min-height",
				(($this.length * $element.height()) + 4)
			);

			label.unbind('click');
			label.click(function()
			{
				elementOptions.functions.click();
			});
		});
	};

	$.fn.quickNav.defaults = {
		callbacks: {
			onLoad: function(e, o){},
			onOpen: function(e, o){},
			onClick: function(e, o){},
			onClose: function(e, o){},
			onMouseover: function(e, o){},
			onMouseout: function(e, o){}
		},
		itemOpenedClassName: "quickNavItemOpened",
		iconClassName: 'quickNavIcon',
		labelClassName: 'quickNavItemLabel',
		contentContainer: '#quickNavContentContainer',
		contentContainerLabel: '#quickNavContentLabel',
		persistOnClick: false,
		animationSpeed: 300,
		containerOpen: false,
		tag: null
	};
})(jQuery);


var Debugger = {
	write: function(message)
	{
		if (window.console && typeof(window.console.log) === 'function') {
			window.console.log(message);
		}
	}
}